package com.bing.springboot.controller;

import com.bing.springboot.bean.Admin;
import com.bing.springboot.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐
 * @version 1.0
 */
@Controller
@Slf4j
public class AdminController {

    /**
     * 响应登录请求
     *
     * @return
     */
    @PostMapping("/login")
    public String login(Admin admin, HttpSession session, Model model) {
        //验证用户是否合法
        if (StringUtils.hasText(admin.getName())
                && admin.getPassword().equals("666")) {
            //合法,重定向到manage.html
            //不适应请求转发是防止刷新页面会重复提交
            //这里老师为什么是写的 manage.html，因为这样可以更加明确的表示到哪个页面
            //manage.html表示要去找 方法的映射路径为 manage.html

            //防止非法登录,将登录用户保存在session
            session.setAttribute("loginAdmin", admin);
            return "redirect:manage.html";


        } else {
            //不合法，重新登录,请求转发
            model.addAttribute("msg", "账号/密码错误");
            return "adminLogin";
        }
    }

    /**
     * 处理用户的请求到 manage.html
     *
     * @return
     */
    @GetMapping("/manage.html")
    public String mainPage(HttpSession session, Model model) {

//        if (session.getAttribute("loginAdmin")!= null){//成功登录过
//            //模拟数据，放入到request域中
//            List<User> users = new ArrayList<>();
//            users.add(new User(1,"关羽","666666",20,"gy@sohu.com"));
//            users.add(new User(2,"张飞","666666",30,"zf@sohu.com"));
//            users.add(new User(3,"赵云","666666",22,"zy@sohu.com"));
//            users.add(new User(4,"马超","666666",28,"mc@sohu.com"));
//            users.add(new User(5,"黄忠","666666",50,"hz@sohu.com"));
//
//            //放入request域中
//            model.addAttribute("users",users);
//
//            //这里才是我们的视图解析到 /templates/manage.html
//            return "manage";
//        }else {
//            //返回登录页
//            model.addAttribute("msg","请先登录");
//            //请求转发到 adminLogin.html
//            return "adminLogin";
//        }

        //使用拦截器验证
        log.info("进入到mainPage（）");
        //模拟数据，放入到request域中
        List<User> users = new ArrayList<>();
        users.add(new User(1, "关羽", "666666", 20, "gy@sohu.com"));
        users.add(new User(2, "张飞", "666666", 30, "zf@sohu.com"));
        users.add(new User(3, "赵云", "666666", 22, "zy@sohu.com"));
        users.add(new User(4, "马超", "666666", 28, "mc@sohu.com"));
        users.add(new User(5, "黄忠", "666666", 50, "hz@sohu.com"));

        //放入request域中
        model.addAttribute("users", users);

        //这里才是我们的视图解析到 /templates/manage.html
        return "manage";
    }

    /**
     * 退出登陆，清空session
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/quit")
    public String quitLogin(HttpSession session, Model model) {
        session.removeAttribute("loginAdmin");

        //回到登陆界面
        return "adminLogin";
    }
}
